import requests
import re
import urllib.parse

url="http://192.168.1.1"
url1='http://192.168.1.1/getpage.gch?pid=1002&nextpage=user_sec_macfilter_conf_t.gch'

headers={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","Cookie" : "_TESTCOOKIESUPPORT=1"}

r=requests.get(url)
#print(r.text)

#result=re.search(r'document.getElementById\("Frm_Logintoken"\).value = "(.*)";',r.text)

#print(result.groups()[0])

r1=r'document.getElementById\("Frm_Logintoken"\).value = "(.*)";'
r2=r'document.getElementById\("Frm_Loginchecktoken"\).value = (\d+);'


def postdata(ck,source):
	jk=re.search(ck,source)
	return jk.groups()[0]


#print(postdata(r2,r.text))


# hh="Frm_Logintoken=" +postdata(r1,r.text) + "&Frm_Loginchecktoken=" + postdata(r2,r.text) + "&_cu_url=0&Right=2&Username=&UserRandomNum=95670920&Password=0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f&action=login";
# print(hh)

hh={"Frm_Logintoken":postdata(r1,r.text),"Frm_Loginchecktoken":postdata(r2,r.text),"_cu_url":0,"Right":2,"Username":'',"UserRandomNum":95670920,"Password":"0b1c0803d1299bcc3f6d4bd997adef8c76a2f234f21fb9cb812d884f1568e88f","action":"login"}


#cookies = dict(_TESTCOOKIESUPPORT=1)

#print(hh)
#print(urllib.parse.urlencode(hh))

#urllib.parse.urlencode(hh)

#response = requests.post(url, data = hh,cookies=cookies)

#print(response.text)

#response = requests.post(url, data = hh,headers=headers)

#print(response.cookies.get_dict())

def getCook(url,hh,headers):
	res=requests.post(url, data = hh,headers=headers)
	uu=res.cookies.get_dict()
	del uu['_TESTCOOKIESUPPORT']
	return uu

print(getCook(url,hh,headers))
dicc=getCook(url,hh,headers)

ret=requests.get(url1,cookies=dicc)#一个字典

r3=r'var session_token = "(\d+)";'

#print(re.search(r3,ret.text).groups()[0])

def jielaihei(url1,data1,headers1):
	requests.post(url1,data=data1,headers=headers1)


def dicttostr(dd):
	return 'SID='+dd['SID']




headers1={"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36","Cookie":dicttostr(dicc)}#不应该是字典的
print(headers1)

data1={'IF_ACTION':'delete','IF_ERRORSTR':'SUCC',
'IF_ERRORPARAM':'SUCC','IF_ERRORTYPE':-1,'IF_INDEX':0,'IF_INSTNUM':1,
'Enable':'','BlackList':'','Type':'','Protocol':'','SrcMacAddr':'',
'DstMacAddr':'','Port':'','Enable0':1,'BlackList0':0,'Type0':'Bridge+Route',
'Protocol0':'ALL','SrcMacAddr0':'18:cf:5e:c4:e7:73',
'DstMacAddr0':'','Port0':0,'IsWanSrvCntl':0,
'IpFilterTarget':0,'UrlFilterTarget':0,'UrlFilterEnable':0,'SrvCntlTarget':1,
'DefaultPolicy':0,'MacFilterTarget':'Discard','MacFilterEnable':1,'_SESSION_TOKEN':re.search(r3,ret.text).groups()[0]}


#IF_ACTION=delete&IF_ERRORSTR=SUCC&IF_ERRORPARAM=SUCC&IF_ERRORTYPE=-1&IF_INDEX=0&IF_INSTNUM=1&Enable=&BlackList=&Type=&Protocol=&SrcMacAddr=&DstMacAddr=&Port=&Enable0=1&BlackList0=0&Type0=Bridge%2BRoute&Protocol0=ALL&SrcMacAddr0=a4%3A4b%3Ad5%3A91%3Aa9%3Ab7&DstMacAddr0=&Port0=0&IsWanSrvCntl=0&IpFilterTarget=0&UrlFilterTarget=0&UrlFilterEnable=0&SrvCntlTarget=1&DefaultPolicy=0&MacFilterTarget=Discard&MacFilterEnable=1&_SESSION_TOKEN=19388008103675298053



#jielaihei(url1)

#IF_ACTION=delete&IF_ERRORSTR=SUCC&IF_ERRORPARAM=SUCC&IF_ERRORTYPE=-1&IF_INDEX=0&IF_INSTNUM=1&Enable=&BlackList=&Type=&Protocol=&SrcMacAddr=&DstMacAddr=&Port=&Enable0=1&BlackList0=0&Type0=Bridge%2BRoute&Protocol0=ALL&SrcMacAddr0=dc%3Af0%3A90%3Ab0%3A3f%3A88&DstMacAddr0=&Port0=0&IsWanSrvCntl=0&IpFilterTarget=0&UrlFilterTarget=0&UrlFilterEnable=0&SrvCntlTarget=1&DefaultPolicy=0&MacFilterTarget=Discard&MacFilterEnable=1&_SESSION_TOKEN=58387584424715169037

#IF_ACTION=delete&IF_ERRORSTR=SUCC&IF_ERRORPARAM=SUCC&IF_ERRORTYPE=-1&IF_INDEX=0&IF_INSTNUM=1&Enable=&BlackList=&Type=&Protocol=&SrcMacAddr=&DstMacAddr=&Port=&Enable0=1&BlackList0=0&Type0=Bridge%2BRoute&Protocol0=ALL&SrcMacAddr0=dc%3Af0%3A90%3Ab0%3A3f%3A88&DstMacAddr0=&Port0=0&IsWanSrvCntl=0&IpFilterTarget=0&UrlFilterTarget=0&UrlFilterEnable=0&SrvCntlTarget=1&DefaultPolicy=0&MacFilterTarget=Discard&MacFilterEnable=1&_SESSION_TOKEN=21060741103773282336

rrt=jielaihei(url1,data1,headers1)
#print(rrt)

print(urllib.parse.urlencode(data1))